<?php
$server = "172.21.147.31";
$connectionInfo = array( "Database"=>"ss2g6", "UID"=>"ss2g6", "PWD"=>"group6",'ReturnDatesAsStrings'=> true  );
$conn = sqlsrv_connect( $server, $connectionInfo );

if ($conn == false){
	 die( print_r( sqlsrv_errors(), true));
}

$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );


if(isset($_GET['update'])){  
    $updateCompanyName = $_GET['companyName'];
    $updateName = $_GET['name'];
    $updateTradeName = $_GET['tradeName'];
    $sql = "SELECT * FROM HasSideEffect WHERE name = '$updateName' AND companyName = '$updateCompanyName' AND tradeName = '$updateTradeName'";
    $result = sqlsrv_query( $conn, $sql, $params, $options );
    $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);
    $updateCompanyName = $row['companyName'];
    $updateName = $row['name'];
    $updateTradeName = $row['tradeName'];
}

if(isset($_POST['Update'])){
    $updateCompanyNameOld = $_POST['companyNameOld'];
    $updateNameOld = $_POST['nameOld'];
    $updateTradeNameOld = $_POST['tradeNameOld'];
    
    $updateCompanyName = $_POST['companyName'];
    $updateName = $_POST['name'];
    $updateTradeName = $_POST['tradeName'];
    
    $sql = "UPDATE HasSideEffect SET name = '$updateName', companyName = '$updateCompanyName', tradeName = '$updateTradeName' WHERE name='$updateNameOld' AND companyName = '$updateCompanyNameOld' AND tradeName = '$updateTradeNameOld'";
    $result = sqlsrv_query( $conn, $sql, $params, $options );
    $confirm = "HasSideEffect updated.";
}

if(isset($_POST['Insert'])) {
    $companyName = $_POST['companyName'];
    $name = $_POST['name'];
    $tradeName = $_POST['tradeName'];
    $sql = "INSERT INTO hasSideEffect VALUES ('$name', '$companyName', '$tradeName')";
    
    $result = sqlsrv_query( $conn, $sql, $params, $options );
	if ($result == false){
		$error_insert = true;
	}
	else{
		$error_insert = false;
	}
    $confirm = "The has side effect relationship is inserted.";
  
}

if(isset($_GET['delete'])){
	$tradeName = $_GET['tradeName'];
	$companyName = $_GET['companyName'];
	$name = $_GET['name'];
	$sql = "DELETE FROM hasSideEffect WHERE companyName = '". $companyName . "' AND tradeName='".$tradeName."' AND name='".$name."'";
	$params = array();
    //$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
    $result = sqlsrv_query( $conn, $sql, $params);
	if($result == false){
		$error = true;
	}
	else {
		$error = false;
	}
	//$rowsAffected = sqlsrv_rows_affected ($result);
}

if (isset($_POST['submit'])){
    $sql1 = $_POST['query'];
}
else
{
    $sql = "SELECT * FROM hasSideEffect";
}
$params = array();
$options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
$stmt = sqlsrv_query( $conn, $sql , $params, $options );
?>

<? include 'html_head.php' ?>
    <? include 'header.php' ?>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="span2">
            <? include 'sidebar.php' ?>
        </div><!--/span-->
        <div class="span9">
        <h2>HasSideEffect</h2>
        <hr>
		<?
		if(isset($_GET['delete'])){
			if ($error){ ?>
				<div class="alert alert-error">
					<? echo "Error in deleting. Foreign Key constraint violated.<br />";?>
				</div>
				
			<? 
			} 
			else { ?>
				<div class="alert alert-success">
				<? echo "Data deleted successfully."?>
				</div>
				
			<?
			} 
		}?>

         <? if(isset($_POST['Update'])){ ?>
			<div class="alert alert-success">
			    <? echo $confirm;?>
			</div>
         <? } ?>
		  <?
			if(isset($_POST['Insert'])){
				if (isset($error_insert) && $error_insert){ ?>
					<div class="alert alert-error">
						<? echo "Data cannot be inserted, foreign key constraint or triggers is violated <br />";?>
					</div>
					
				<? 
				} 
				else { ?>
					<div class="alert alert-success">
					<? echo $confirm;?>
					</div>
					
				<?
				} 
			}?>
         <?
         
		if(sqlsrv_has_rows($stmt))
		{		
			$row_count = sqlsrv_num_rows($stmt);
            $colnames = sqlsrv_field_metadata($stmt)
		?>
		
		<table class="table table-striped table table-condensed">
		
		<thead>
			<tr>
              <?
					echo "<th>Name</th>";
                    echo "<th>Company Name</th>";
					echo "<th>Trade Name</th>";

                ?>
			</tr>	
			</thead>
		<tbody>
		<?
			while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)){
        ?>
			<tr>
               <?
					foreach ($colnames as &$colname){
					$cellname = $colname['Name'];
					echo "<td>";
					if ($colname['Type'] == 93){
						$temp_date = explode(" ", $row[$cellname]);
						echo $temp_date[0];
					}
					else{
						echo $row[$cellname];
					}
					echo "</td>";
					}
					$name = urlencode($row['name']);
					$tradeName = urlencode($row['tradeName']);
					$companyName = urlencode($row['companyName']);
					echo "<td><a href='sideeffects.php?delete=true&tradeName=" . $tradeName . "&companyName=" . $companyName . "&name=" . $name . "'>Delete</a></td>";
					echo "<td><a href='sideeffects.php?update=true&tradeName=" . $tradeName . "&companyName=" . $companyName . "&name=" . $name . "#updates'>Update</a></td>";
				?>
			</tr>
		  
		<?
			}
		?>
		</tbody>
		</table>
		<?
		}
		else{
			echo "<h2>No records.</h2>";
		}
		?>
          <div class="row-fluid">
            <div class="span12">

            <? if (isset($_GET['update'])){ ?>
                <a name="updates"></a><h3>Update HasSideEffect information:</h3>

                <form method="post" action="sideeffects.php" class="well form-horizontal">
                    <input type="hidden" name="companyNameOld" value="<? echo $updateCompanyName; ?>" />
                    <input type="hidden" name="tradeNameOld" value="<? echo $updateTradeName; ?>" />
                    <input type="hidden" name="nameOld" value="<? echo $updateName; ?>" />
                    <div class="control-group">
                        <label class="control-label">Company Name</label>
                        <div class="controls">
                            <? 
								$sql2 = "SELECT companyName FROM PharmaceuticalCompany order by companyName ASC";
								$result = sqlsrv_query($conn,$sql2);
								echo "<select name = 'companyName'>";
								while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
                                    echo "<option";
                                    if($data['companyName'] == $updateCompanyName){
                                        echo " selected=selected";
                                    }
                                    echo ">";
									echo $data['companyName']; 
									echo "</option>";
								}
								echo "</select>";
							?>                            
                            <!--<input type="text" name="companyName" id="companyName" placeholder="eg. Alibaba Medical Centre" />-->
                            <span class="help-inline"><a href="pharmaceutical.php" target="_blank">Create new PharmaceuticalCompany</a></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Name</label>
                        <div class="controls">
                        	<? 
								$sql2 = "SELECT name FROM SideEffect order by name ASC";
								$result = sqlsrv_query($conn,$sql2);
								echo "<select name = 'name'>";
								while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
                                    echo "<option";
                                    if($data['name'] == $updateName){
                                        echo " selected=selected";
                                    }
                                    echo ">";
									echo $data['name']; 
									echo "</option>";
								}
								echo "</select>";
							?>
                            <!--<input type="text" name="name" id="name" placeholder="eg. Headache" />-->
                            <span class="help-inline"><a href="side.php" target="_blank">Create new SideEffect</a></span>
                        </div> 
                    </div>
                    <div class="control-group">
                        <label class="control-label">Trade Name</label>
                        <div class="controls">
                        	<? 
								$sql2 = "SELECT tradeName FROM Drug order by tradeName ASC";
								$result = sqlsrv_query($conn,$sql2);
								echo "<select name = 'tradeName'>";
								while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
                                    echo "<option";
                                    if($data['tradeName'] == $updateTradeName){
                                        echo " selected=selected";
                                    }
                                    echo ">";
									echo $data['tradeName']; 
									echo "</option>";
								}
								echo "</select>";
							?>
                            <!--<input type="text" name="tradeName" id="tradeName" placeholder="eg. Panadol" />-->
                            <span class="help-inline"><a href="drugs.php" target="_blank">Create new Drug</a></span>
                        </div>                    
                    </div>
                    <div class="form-actions">
                        <input type="submit" name="Update" value="Update"  class="btn btn-primary"/>
                    </div> 
                    </form>


            <? } ?>


            <h3>Create a new HasSideEffect information:</h3> 
                    <form method="post" action="sideeffects.php" class="well form-horizontal">
                    <div class="control-group">
                        <label class="control-label">Company Name</label>
                        <div class="controls">
                            <? 
								$sql2 = "SELECT companyName FROM PharmaceuticalCompany order by companyName ASC";
								$result = sqlsrv_query($conn,$sql2);
								echo "<select name = 'companyName'>";
								while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
									echo "<option>";
									echo $data['companyName']; 
									echo "</option>";
								}
								echo "</select>";
							?>                            
                            <!--<input type="text" name="companyName" id="companyName" placeholder="eg. Alibaba Medical Centre" />-->
                            <span class="help-inline"><a href="pharmaceutical.php" target="_blank">Create new PharmaceuticalCompany</a></span>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">Name</label>
                        <div class="controls">
                        	<? 
								$sql2 = "SELECT name FROM SideEffect order by name ASC";
								$result = sqlsrv_query($conn,$sql2);
								echo "<select name = 'name'>";
								while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
									echo "<option>";
									echo $data['name']; 
									echo "</option>";
								}
								echo "</select>";
							?>
                            <!--<input type="text" name="name" id="name" placeholder="eg. Headache" />-->
                            <span class="help-inline"><a href="side.php" target="_blank">Create new SideEffect</a></span>
                        </div> 
                    </div>
                    <div class="control-group">
                        <label class="control-label">Trade Name</label>
                        <div class="controls">
                        	<? 
								$sql2 = "SELECT tradeName FROM Drug order by tradeName ASC";
								$result = sqlsrv_query($conn,$sql2);
								echo "<select name = 'tradeName'>";
								while ($data=sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
									echo "<option>";
									echo $data['tradeName']; 
									echo "</option>";
								}
								echo "</select>";
							?>
                            <!--<input type="text" name="tradeName" id="tradeName" placeholder="eg. Panadol" />-->
                            <span class="help-inline"><a href="drugs.php" target="_blank">Create new Drug</a></span>
                        </div>                    
                    </div>
                    <div class="form-actions">
                        <input type="submit" name="Insert" value="Insert"  class="btn btn-primary"/>
                    </div> 
                    </form>
               <h3>Enter your query:</h3>
                    <form method="post" action="custom.php" class="well form-inline">
                    <div class="control-group">
                        <div class="controls">
                            <textarea name="query" class="span10" id="textarea" rows="5" placeholder = 'SQL query'></textarea>
                        </div>
                    </div>
                    <p>
                    <p><input type="submit" name="submit" class="btn btn-primary" href="#" /></p>
                    </form>
                    
            </div><!--/span-->
          </div><!--/row-->
        </div><!--/span-->
      </div><!--/row-->

      <hr>

      <footer>
        <p>&copy; SS2 GROUP 6 </p>
      </footer>

    </div><!--/.fluid-container-->
  </body>
</html>
